/**
 * ScreenShot.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-4-3 上午10:55:27
 */
package com.ghyf.mplay.util;
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
   
import android.app.Activity;  
import android.graphics.Bitmap;  
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;  
import android.view.Display;
import android.view.View; 
/**
 * 屏幕截图 通过此方法有播放视频的时候截屏是黑屏
 * @author cuiran
 * @version 1.0.0
 */
public class ScreenShot {
	
	/**
	 * 截取内层边框中的View.
	 */
	@SuppressWarnings("unused")
	private static Bitmap getCutImage(Activity activity) {
		View view = activity.getWindow().getDecorView();
		Bitmap bmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bmp);
		view.draw(canvas);
		return bmp;
	}

	// 获取指定Activity的截屏，保存到png文件  
    private static Bitmap takeScreenShot(Activity activity){  
        //View是你需要截图的View  
        View view = activity.getWindow().getDecorView();  
        view.setDrawingCacheEnabled(true);  
        view.buildDrawingCache();  
        Bitmap b1 = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(b1);
		view.draw(canvas);
//        Bitmap b1 = view.getDrawingCache();  
           
        //获取状态栏高度  
        Rect frame = new Rect();    
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);    
        int statusBarHeight = frame.top;    
        System.out.println(statusBarHeight);  
        Display display= activity.getWindowManager().getDefaultDisplay();
		Point size=new Point();
		display.getSize(size);
		int width = size.x;
		int height = size.y;
         height=1080;
        //去掉标题栏  
        //Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);  
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);  
        view.destroyDrawingCache();  
        return b;  
    }  
       
    //保存到sdcard  
    private static void savePic(Bitmap b,String strFileName){  
        FileOutputStream fos = null;  
        try {  
            fos = new FileOutputStream(strFileName);  
            if (null != fos)  
            {  
                b.compress(Bitmap.CompressFormat.PNG, 90, fos);  
                fos.flush();  
                fos.close();  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
       
    //程序入口  
    public static void shoot(Activity a){  
        ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/test.png");  
    }  
}
